package com.example.administrator.test0211;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by scenele on 17/2/12.
 * 按两次退出程序
 */
public class MainActivity extends AppCompatActivity {
    private static boolean isExit=false;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.tv);
       textView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               exitBy2Click();//按两次退出程序。
           }
       });
    }
    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            showToast("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
        }
    }
    public void showToast(String msg) {
        ToastUtils.showToast(this, msg);
    }
}
